package com.eke.dao;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eke.bean.Article;
import com.eke.bean.Users;
import com.jason.dbhelper2.jdbc.BaseJdbcTemplate;

@Repository("usersDao")
public class UsersDao extends BaseJdbcTemplate<Users> {
	/**
	 * 添加用户
	 *   
	 * @param users
	 * @return int
	 */
	public int adduser(Users users) {
		return this.insert(users);
	}

	/**
	 * 通过用户名查询用户
	 * 
	 * @param userName
	 * @return {@link List}<{@link Users}>
	 */
	public List<Users> qurryUser(String userName) {
		List<Users> ls = (List<Users>) this.queryForList("Select * from users where user_name = ?", userName);
		return ls;
	}

	/**
	 * 根据userId查询用户信息
	 * 
	 * @param userId
	 * @return {@link Users}
	 */
	public Users queryUserByUserId(Integer userId) {
		return this.queryForObject("select * from users where USER_ID = ?", userId);
	}

	/**
	 * 修改用户积分
	 * 
	 * @param user_integral
	 *            修改后的用户积分值
	 * @return {@link Boolean}
	 */
	public boolean updateUsersIntegral(Integer user_integral, Integer userId) {
		return this.update("update users set user_integral = ? where user_id = ?", user_integral, userId) > 0;
	}

	/**
	 * 查询所有用户
	 * 
	 * @return {@link List}<{@link Users}>
	 */
	public List<Users> queryAllUser(int startRow, int pageSize) {
		return this.queryForList("SELECT * FROM users ORDER BY user_id DESC limit " + startRow + "," + pageSize);
	}

	/**
	 * 根据用户名查询用户
	 * 
	 * @param userName
	 * @param startRow
	 * @param pageSize
	 * @return {@link List}<{@link Users}>
	 */
	public List<Users> qurryUser(String userName, int startRow, int pageSize) {
		// List<Users> ls = (List<Users>) this.queryForList("Select * from users
		// where user_name = '%?%'", userName);
		return this.queryForList("Select * from users where user_name like '%" + userName
				+ "%' ORDER BY user_id DESC limit " + startRow + "," + pageSize);
	}

	/**
	 * 发布问题减少积分
	 * 
	 * @param user_integral
	 *            问题积分值
	 * @return {@link Boolean}
	 */
	public boolean addQuestionUpdateIntegral(Integer user_integral, Integer userId) {
		return this.update("update users set user_integral = user_integral - ? where user_id = ?", user_integral,
				userId) > 0;
	}

	/**
	 * 判断用户积分是否足够
	 * 
	 * @param userId
	 * @return {@link Users}
	 */
	public Users queryUserIntegral(Integer userId) {
		return this.queryForObject("SELECT user_integral from users where user_id = ?", userId);
	}

	/**
	 * 根据用户名查询用户
	 * 
	 * @param Title
	 * @param startRow
	 * @param pageSize
	 * @return {@link List}<{@link Users}>
	 */
	public List<Users> queryUserFy(String Title, int startRow, int pageSize) {
		try {
			Title = new String(Title.getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql = "select * from users WHERE user_state = 1 and user_name LIKE '%" + Title
				+ "%' ORDER BY user_id DESC limit " + startRow + "," + pageSize;
		return this.queryForList(sql);
	}
}
